package com.shop.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.model.entity.BulkTb;
import com.shop.model.entity.GoodsTb;
import com.shop.model.entity.OrderTb;
import com.shop.model.mapper.BulkTbMapper;
import com.shop.model.service.IBulkTbService;
import com.shop.model.service.IGoodsTbService;
import com.shop.model.service.IOrderTbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *这是团购表
 * @author shop
 * @since 2018-12-03
 */
@Controller
public class BulkTbController {

    @Autowired
    private IBulkTbService iBulkTbService;

    @Autowired
    private BulkTbMapper bulkTbMapper;

    @Autowired
    private IOrderTbService iOrderTbService;

    @Autowired
    private IGoodsTbService iGoodsTbService;
    @RequestMapping("/bulk")
    public String bulk(HttpServletRequest request, Model model) {
        //今日团购
        int pageSize=3;//默认页面显示条数是3
        //默认当前页
        int nowPage=1;
        //获取前段传进来的当前页,如果有值说明不是第一次访问根据传值确定当前页
        System.out.println("传进来的nowpage======"+request.getParameter("nowPage"));
        if(null!=request.getParameter("nowPage")){
            nowPage=Integer.valueOf(request.getParameter("nowPage"));
        }
        //将团购对象与对应的商品对象封装在map里面
        HashMap<BulkTb,GoodsTb> hashMap=new HashMap();
        System.out.println( LocalDate.now());
        //通过今天的日期获取数据库团购表今日商品的数据
        List<BulkTb> list = iBulkTbService.list(new QueryWrapper<BulkTb>()
                .eq("bulk_date",LocalDate.now()));
//        //获取团购表数据总数
        int count=list.size();
//        //通过  当前页 页面条数  进行分页查询bulkTbMapper.selectPage(page,tiaojian)
        IPage iPage = bulkTbMapper.selectPage(new Page(nowPage, 5), new QueryWrapper<BulkTb>().eq("bulk_date", LocalDate.now()));
        List<BulkTb> todayList = iPage.getRecords();
        System.out.println("目前数据------------------------");
        System.out.println("目前的nowpage----------------"+nowPage);
        for (BulkTb bulkTb : todayList) {
            System.out.println(bulkTb.toString());
        }
        System.out.println("数据结束-------------");
        System.out.println("today-----"+list.size());
        for (BulkTb bulkTb : todayList) {
            //获取团购数据的商品id字段
            Integer bulkGoodsId = bulkTb.getBulkGoodsId();
            //通过商品id商品表对应id数据
            GoodsTb goodsTb = iGoodsTbService.getById(bulkGoodsId);
            //将两个对象添加到map
            hashMap.put(bulkTb,goodsTb);
        }
        model.addAttribute("todayBulkHashMap",hashMap);
        model.addAttribute("count",count);
        model.addAttribute("nowPage",nowPage);
        System.out.println("长度---"+hashMap.size());



        //明日预告
        HashMap<BulkTb,GoodsTb> thashMap=new HashMap();
        //通过明天的日期获取数据库团购表今日商品的数据
        List<BulkTb> list1 = iBulkTbService.list(new QueryWrapper<BulkTb>()
                .eq("bulk_date",LocalDate.now().plusDays(1)));
        //只拿四条显示
        List<BulkTb> list2=new ArrayList<>();
        if(list1.size()>4){
            for (int i = 0; i <4; i++) {
                list2.add(list1.get(i));
            }
        }else{
            list2=list1;
        }
        System.out.println("t y/n 4------"+list2.size());
        for (BulkTb bulkTb1 : list2) {
            //获取团购数据的商品id字段
            Integer bulkGoodsId1 = bulkTb1.getBulkGoodsId();
            //通过商品id商品表对应id数据
            GoodsTb goodsTb1 = iGoodsTbService.getById(bulkGoodsId1);
            //将两个对象添加到map
            thashMap.put(bulkTb1,goodsTb1);
        }
        model.addAttribute("tomorrowBulkHashMap",thashMap);
        System.out.println("t长度----"+thashMap.size());


        //团购销量榜
        /**
         * 获取订单表中order_process（流程）字段为6（已收货）和order_status（订单状态）字段为3（已完成）的商品
         * 通过该订单商品的order_goods_id字段去查团购表的bulk_goods_id字段  如果有相匹配的商品，拿3条出来显示
         * ！！！！！！！！！这里没有考虑销量
//         */
        HashMap<BulkTb,GoodsTb> l=new HashMap();
        List<OrderTb> orderlist= iOrderTbService.list(new QueryWrapper<OrderTb>()
                .eq("order_process", 6)
                .eq("order_status", 3));
        System.out.println("-------已完成的订单数量："+orderlist.size());
        int count1=1;
        for (OrderTb orderTb : orderlist) {
            //获取订单表商品id
            int orderGoodsId = orderTb.getOrderGoodsId();
            //通过这个商品id查询团购表里面有没有这个商品,实际上这个list里面只有一个商品
            List<BulkTb> bulk_BulkTb = iBulkTbService.list(new QueryWrapper<BulkTb>().eq("bulk_goods_id", orderGoodsId));
            for (BulkTb bulkTb : bulk_BulkTb) {
                GoodsTb goodsbyId1 = iGoodsTbService.getById(orderGoodsId);
                l.put(bulkTb,goodsbyId1);
            }
            count1++;
            if(count1==4){
                break;
            }

        }
        System.out.println("符合要求的团购销量："+l.size());
        model.addAttribute("SalesVolume",l);
        return "buytoday";
    }

}

